package cn.niit.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxyFactory {
    private Object target;

    public DynamicProxyFactory(Object target) {
        this.target = target;
    }

    /**
     * 生成代理类
     */
    public Object getProxy() {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("【getProxy】开始执行" + method.getName() + "方法...");
                        Object returnVal = method.invoke(target, args);
                        System.out.println("【getProxy】完成" + method.getName() + "方法的执行..." + "\n");
                        return returnVal;
                    }

                });
    }
}
